﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.LoaiThietBiService;
namespace eTMS.Controllers{
public class LoaiThietBiControllerBase : CommonController{
	public LoaiThietBiControllerBase()
		: base()
	{	
		this.dao = new LoaiThietBiServiceClient();
	}
	~LoaiThietBiControllerBase(){
		dao.Close();
	}
	private DataTable _Table;
	private LoaiThietBiEntity oLoaiThietBi;	
	public LoaiThietBiEntity LoaiThietBiObject{
		get{
			return oLoaiThietBi;
		}
		set{
			oLoaiThietBi=value;
		}
	}
	protected LoaiThietBiServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSLoaiThietBi(false);			
			return _Table;
		}
	}
	public void LoadAllDSLoaiThietBi(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oLoaiThietBi==null)return false;
		bool result=true;
		try{
			oLoaiThietBi = dao.SaveRet(oLoaiThietBi);
		}
		catch{
			result=false;
		}
		if(result){
			_ListLoaiThietBi.Add(oLoaiThietBi);
		}
		return result;
	}
	public bool Update(){
		if(oLoaiThietBi==null)return false;
		return dao.Update(oLoaiThietBi);
	}
	public bool Delete(){
		if(oLoaiThietBi==null)return false;
		bool retvalue = dao.Delete(oLoaiThietBi);
		if(retvalue){			
			_ListLoaiThietBi.Remove(oLoaiThietBi);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 LoaiThietBi_ID){
		bool retvalue = dao.DeleteByID(LoaiThietBi_ID);		
		if(retvalue){
			LoaiThietBiEntity oDeleted=FindLoaiThietBi(LoaiThietBi_ID);
			if(oDeleted!=null){
				_ListLoaiThietBi.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oLoaiThietBi = new LoaiThietBiEntity();
	}
	//Check if exists MaLoaiThietBi
	public bool CheckExistsMaLoaiThietBi(object value){	
		return dao.SelectOneByMaLoaiThietBi(value.ToString()).MaLoaiThietBi!=null;
    }    
    //Copy data from data row in grid to object
    public LoaiThietBiEntity MapDataRowToObject(DataRow row){
		LoaiThietBiEntity o = new LoaiThietBiEntity();
		 o.MaLoaiThietBi=Convert.ToString(row["MaLoaiThietBi"]);
		 o.TenLoaiThietBi=Convert.ToString(row["TenLoaiThietBi"]);
		 if(!row.IsNull("LoaiThietBi_ID"))
		    o.LoaiThietBi_ID=(Int32)(row["LoaiThietBi_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(LoaiThietBiEntity o,DataRow row){
		row["MaLoaiThietBi"]=o.MaLoaiThietBi;
		row["TenLoaiThietBi"]=o.TenLoaiThietBi;
		row["LoaiThietBi_ID"]=o.LoaiThietBi_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.LoaiThietBiObject,row);
    }
	public void LoadObject(Int32 LoaiThietBi_ID){
		LoaiThietBiEntity oLoad=FindLoaiThietBi(LoaiThietBi_ID);
		if(oLoad==null){
			oLoaiThietBi=this.LoadLoaiThietBi(LoaiThietBi_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oLoaiThietBi.IsNullLoaiThietBi_ID;
			if(!bNotIsNull){
				this.ListLoaiThietBi.Add(oLoaiThietBi);
			}
		}
		else{
			oLoaiThietBi=oLoad;
		}
	}
	#region CachedList LoaiThietBi
	private static List<LoaiThietBiEntity> _ListLoaiThietBi=new List<LoaiThietBiEntity>();
    public List<LoaiThietBiEntity>ListLoaiThietBi{
		get{
			return _ListLoaiThietBi;
		}
	}
	public static List<LoaiThietBiEntity>ListLoaiThietBiStatic{
		get{
			return _ListLoaiThietBi;
		}
	}
	public LoaiThietBiEntity FindLoaiThietBi(Int32 LoaiThietBi_ID){
		LoaiThietBiEntity oResult=this.ListLoaiThietBi.Find(delegate(LoaiThietBiEntity oFind){
			bool result=true; 
			result&=oFind.LoaiThietBi_ID.Equals(LoaiThietBi_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllLoaiThietBi(bool clearCache){
		if(_ListLoaiThietBi.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		LoaiThietBiServiceClient dao = new LoaiThietBiServiceClient();
		_ListLoaiThietBi=new List<LoaiThietBiEntity>(dao.SelectAll());
		dao.Close();
	}
	public LoaiThietBiEntity LoadLoaiThietBi(Int32 LoaiThietBi_ID){			
		LoaiThietBiEntity o=dao.SelectOne(LoaiThietBi_ID);		
		return o;
	}
	#endregion
}
}
